uniform sampler2D ge_Texture1;

smooth in vec3 normal, vVertex, eyeVec;
vec3 N;

vec4 calculateLight(ge_struct_Light light, float spec){
	vec4 final_color = light.ambient;
	if(light.attenuation == -1.0){
		return vec4(0.0);
	}else{
		vec3 lVec = light.position.xyz - vVertex;
		vec3 bump = 2.0 * normalize( texture(ge_Texture1, ge_TexCoord0.st).rgb * 2.0 - 1.0);
		float diffuse = max( dot((lVec)*inversesqrt(dot(lVec, lVec)), bump), 0.0 );
		if(diffuse > 1.0){
			diffuse = 1.0;
		}

		vec3 L = normalize(light.vector - vVertex);
		float lambertTerm = dot(N, L);
		if(lambertTerm > 0.0){
			final_color += light.diffuse * lambertTerm * diffuse;
			/*
			if(spec > 0.0){
				vec3 E = normalize(eyeVec);
				vec3 R = reflect(-L, N);
				float specular = pow(max(dot(R, E), 0.0), 2.0);
				final_color += spec * vec4(specular);
			}
			*/
		}
	}

	final_color.a = 1.0;
	return final_color;
}

void main(){
	vec4 tex = texture(ge_Texture, ge_TexCoord0.st);
	if(tex.a < 0.4){
		discard;
	}
	N = normalize(normal);
	vec4 light = calculateLight(ge_StaticLights[0], 0.0);
	ge_FragColor = ge_Color * tex * light;
}
